<?php

try
{

    $viewer = Engine_Api::_() -> user() -> getViewer();

    if (!$viewer -> getIdentity())
    {
        exit('{notification:0,freq:0,msg: ""}');
    }
    
    $notificationTb = Engine_Api::_() -> getDbtable('notifications', 'activity');
    $tbName = $notificationTb -> info('name');
    $db = $notificationTb -> getAdapter();
    $notif = $db -> query("SELECT COUNT(*) AS total FROM $tbName WHERE `user_id` = {$viewer->getIdentity()} AND `read` = 0 AND `type` <> 'friend_request' AND `type` <> 'message_new'") -> fetch();
    $freq = $db -> query("SELECT COUNT(*) AS total FROM $tbName WHERE `user_id` = {$viewer->getIdentity()} AND `read` = 0 AND `type` = 'friend_request'") -> fetch();
    $data = array(
        'notification' => $notif['total'],
        'freq' => $freq['total'],
        'msg' => Engine_Api::_() -> messages() -> getUnreadMessageCount($viewer)
    );
    echo Zend_Json::encode($data);

}
catch(exception $e)
{
    echo '{notification:0,freq:0,msg: ""}';
}